﻿#include "mainservice.h"

MainService::MainService(QObject *parent)
    : QObject{parent}
{
    mQmlSerialPortService      = QmlSerialPort::getInstance();
    mQmlRelaySwitchService     = QmlRelaySwitch::getInstance();
    mQmlPorxyConfigService     = QmlPorxyConfig::getInstance();
    mQmlLogsBrowserService     = QmlLogsBrowser::getInstance();
    // Http Server
    mHttpServerService         = HttpServer::getInstance();
}

MainService::MainService(QGuiApplication *app, QQmlApplicationEngine *engine, QObject *parent)
    : QObject{parent}
{
    (void)app;
    (void)engine;
    mQmlSerialPortService      = QmlSerialPort::getInstance();
    mQmlRelaySwitchService     = QmlRelaySwitch::getInstance();
    mQmlPorxyConfigService     = QmlPorxyConfig::getInstance();
    mQmlLogsBrowserService     = QmlLogsBrowser::getInstance();
    // Http Server
    mHttpServerService         = HttpServer::getInstance();
    // TODO init app and engine
    if (engine) {
        qmlRegisterType<QmlSerialPort>("QmlSerialPort", 1, 0, "QmlSerialPort");
        // qmlRegisterSingletonInstance("mQmlRelaySwitchService", 1, 0, "mQmlRelaySwitchService", mQmlRelaySwitchService);
        engine->rootContext()->setContextProperty("mQmlSerialPortService", mQmlSerialPortService);
        engine->rootContext()->setContextProperty("mQmlRelaySwitchService", mQmlRelaySwitchService);
        engine->rootContext()->setContextProperty("mQmlPorxyConfigService", mQmlPorxyConfigService);
        engine->rootContext()->setContextProperty("mQmlLogsBrowserService", mQmlLogsBrowserService);
    }
    // Connecting Signal and Slot
    connect(mQmlPorxyConfigService, &QmlPorxyConfig::startSignal, this, &MainService::qmlPorxyConfig_startSlot);
    connect(mQmlPorxyConfigService, &QmlPorxyConfig::stopSignal, this, &MainService::qmlPorxyConfig_stopSlot);
    connect(mHttpServerService, &HttpServer::requestInd, mQmlRelaySwitchService, &QmlRelaySwitch::syncSwitchStatus2QmlSlot);
    connect(mQmlRelaySwitchService, &QmlRelaySwitch::pushSwitchStatus2QmlSerialPortSignal,
            mQmlSerialPortService, &QmlSerialPort::pushSwitchStatus2QmlSerialPortSlot);
    // Connecting Logcats Signal and Slot
    connect(mQmlSerialPortService, &QmlSerialPort::logsPrintSignal, mQmlLogsBrowserService, &QmlLogsBrowser::logsPrintSlot);
    connect(mQmlRelaySwitchService, &QmlRelaySwitch::logsPrintSignal, mQmlLogsBrowserService, &QmlLogsBrowser::logsPrintSlot);
    connect(mQmlPorxyConfigService, &QmlPorxyConfig::logsPrintSignal, mQmlLogsBrowserService, &QmlLogsBrowser::logsPrintSlot);
    connect(mHttpServerService, &HttpServer::logsPrintSignal, mQmlLogsBrowserService, &QmlLogsBrowser::logsPrintSlot);

    // init other settings
    mQGuiApplicationService = app;
    mQQmlApplicationEngineService = engine;
    app->setOrganizationName("TimeChicken");
    app->setOrganizationDomain("timechicken.cc");
    app->setApplicationName("Uart2Relay");
}

MainService *MainService::getInstance()
{
    static MainService *myMainService = new MainService();
    return myMainService;
}

MainService *MainService::getInstance(QGuiApplication *app, QQmlApplicationEngine *engine)
{
    static MainService *myMainService = new MainService(app, engine);
    return myMainService;
}

void MainService::qmlPorxyConfig_startSlot(int port)
{
    int ret = mHttpServerService->start(port);
    if (ret == 0) {
        mQmlPorxyConfigService->setPorxyState(true);
    }
}

void MainService::qmlPorxyConfig_stopSlot()
{
    mHttpServerService->stop();
    mQmlPorxyConfigService->setPorxyState(false);
}

void MainService::start()
{
#ifdef QT_DEBUG
    mQmlLogsBrowserService->log("This is the debug version. Build: %s %s", __DATE__, __TIME__);
#else
    mQmlLogsBrowserService->log("This is the release version. Build: %s %s", __DATE__, __TIME__);
#endif
}

void MainService::log(QString s)
{
    QString logs = "[MainService]: " + s;
    emit logsPrintSignal(logs);
    qDebug() << logs;
}
